package com.alog.yzd.jdbc.hm;

import com.alog.yzd.utils.Print;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

public class DateTest {
    public static void main(String[] args){
        //create(5, "Papper","dddd",new Date());
        read(5);
    }

    static void create(int id, String name, String pwd, Date birth){
        String sql = "insert into user (id, name, pwd, birth) values (?,?,?,?);";
        Connection connction = null;
        PreparedStatement preparedStatement = null;
        try {
            connction = JdbcUtils2.getConnction();
            preparedStatement = connction.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            preparedStatement.setString(2,name);
            preparedStatement.setString(3,pwd);
            preparedStatement.setDate(4, new java.sql.Date(birth.getTime()));
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils2.release(null, preparedStatement,connction);
        }
    }

    static void read(int id){
        String sql = "select id, name, pwd, birth from user where id = ?";
        Connection conn = null;
        PreparedStatement ps = null ;
        ResultSet rs = null;
        try{
            conn = JdbcUtils2.getConnction();
            ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            rs = ps.executeQuery();
            while(rs.next()){
                Date date = rs.getDate("birth");
                Print.print(date);
            }
        } catch(SQLException e){
            JdbcUtils2.release(rs,ps,conn);
        }
    }
}
